package com.lin.reggie.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lin.reggie.common.JacksonObjectMapper;
import com.lin.reggie.entity.Employee;
import com.lin.reggie.service.EmployeeService;
import com.lin.reggie.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.websocket.server.PathParam;
import java.time.LocalDateTime;
import java.util.Date;

/**
 * <p>
 * 员工信息 前端控制器
 * </p>
 *
 * @author lin
 * @since 2022-05-26
 */
@Controller
@Slf4j
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    EmployeeService employeeService;

    @PostMapping("login")
    @ResponseBody
    public R<Employee> login(HttpServletRequest request, @RequestBody Employee employee){
        //1.将页面提交的密码password进行md5加密
        String password = employee.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        //2.根据用户名查询用户是否存在
        String username = employee.getUsername();
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Employee::getUsername,username);
        Employee oneEmployee = employeeService.getOne(queryWrapper);
        //3.如果没有查询到返回登录失败界面
        if (oneEmployee==null){
            return R.error("登录失败");
        }
        //4.存在用户进行密码比对
        if (password.equals(oneEmployee.getPassword())){
            return R.error("密码错误");
        }
        //5.密码比对成功，查看员工状态
        if (oneEmployee.getStatus()==0){
            return R.error("账号异常");
        }
        //6.登录成功，将id放入session
        HttpSession session = request.getSession();
        session.setAttribute("employee",oneEmployee.getId());
        return R.success(oneEmployee);
    }
    @PostMapping("/logout")
    @ResponseBody
    public R<String> logout(HttpServletRequest request){
        //清理session中的员工id
        request.getSession().removeAttribute("employee");
        return R.success("退出成功");
    }
    @PostMapping
    @ResponseBody
    public R<String> save(HttpServletRequest request, @RequestBody Employee employee){
        log.info("新增员工信息",employee.toString());
        //设置初始密码
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
//        employee.setCreateTime(LocalDateTime.now());
//        employee.setUpdateTime(LocalDateTime.now());
//        Long empId = (Long)request.getSession().getAttribute("employee");
//        employee.setCreateUser(empId);
//        employee.setUpdateUser(empId);
        employeeService.save(employee);
        return R.success("修改员工成功");
    }
//    @GetMapping("/page")
//    public R list(
//                  @RequestParam(name = "pageNum", defaultValue = "1") Integer pageNum,
//                  @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize){
//
//
//        return employeeService.
//
//    }
    @GetMapping("/page")
    @ResponseBody
    public R<Page> page(int page,int pageSize,String name){
        //1.ajax传递请求参数
        log.info("page={},pageSize={},name={}",page,pageSize,name);

        //构造分页构造器
        Page pageInfos = new Page(page, pageSize);
        //构造一个条件构造器
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotEmpty(name),Employee::getName,name);
        //添加一个排序条件
        queryWrapper.orderByDesc(Employee::getUpdateTime);

        //执行查询,page方法是分页查询，传入构造的分页器和条件构造器
        employeeService.page(pageInfos, queryWrapper);
        System.out.println(pageInfos.toString());
        return R.success(pageInfos);
    }

    //修改员工状态,put请求，和新增员工有差别
    @PutMapping()
    @ResponseBody
    public R<String> updateStatus(HttpServletRequest httpServletRequest,@RequestBody Employee employee){
        log.info(employee.toString());
//        //Long型只有16位，在jsp中会导致四舍五入失准
//        Long id = employee.getId();
//        employee.setUpdateTime(LocalDateTime.now());
//        employee.setUpdateUser((Long) httpServletRequest.getSession().getAttribute("employee"));
        employeeService.updateById(employee);
        return R.success("修改成功");
    }
    /*
    根据编辑按钮传过来的id获得要修改的员工信息
     */
    @GetMapping("/{id}")
    @ResponseBody
    @Transactional
    public R<Employee> getById(@PathVariable("id") Long id){
        log.info("根据id查询员工信息"+id);
        Employee employee =employeeService.getById(id);
        if (employee!= null) {
            return R.success(employee);
        }
        return R.error("查询失败，不存在该用户");
    }
    /*
        成功提交回数据之后，提交按钮自动跳到update,实现添加
     */
}

